package com.asistencia.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.asistencia.dao.interfaces.CareerDao;
import com.asistencia.domain.Career;
import com.asistencia.domain.Student;

@Repository
@SuppressWarnings({ "rawtypes", "unchecked" })
public class CareerJdbc implements CareerDao{

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<Career> listAll() throws DataAccessException {
		String SQL="SELECT * FROM career";
		List matches=jdbcTemplate.query(SQL, new RowMapper(){
			
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
				Career career= new Career();
				career.setIdCareer(rs.getInt("idcareere"));
				career.setName(rs.getString("name"));
				career.setCareerCode(rs.getString("career_code"));
				
				return career;
				
			}
			});
			
			return (matches.size() > 0 ? (List<Career>) matches : null);
	}
	
			@Override
	public List<Career> listByName(String name) throws DataAccessException {
		String SQL="SELECT * FROM career WHERE name LIKE ?";
		List matches =jdbcTemplate.query(SQL, new Object[] {"%"+name+"%", "%"+name+"%"}, new RowMapper(){
			
@Override
	public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
				Career career= new Career();
				career.setIdCareer(rs.getInt("idcareere"));
				career.setName(rs.getString("name"));
				career.setCareerCode(rs.getString("career_code"));
				
				return career;
			}
			});
			
			return (matches.size() > 0 ? (List<Career>) matches : null);
		}
	

	@Override
	public Career getById(Integer idCareer) throws DataAccessException {
		String SQL="SELECT * FROM career WHERE idcarreer=?";
		List matches =jdbcTemplate.query(SQL, new Object[]{idCareer}, new RowMapper(){
			
@Override
	public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
				Career career= new Career();
				career.setIdCareer(rs.getInt("idcarreer"));
				career.setName(rs.getString("name"));
				career.setCareerCode(rs.getString("career_code"));
				
				return career;
			}
			});
			
		return (matches.size() > 0 ? (Career) matches.get(0) : null);
		}}
	
